package androidvn.xenforo.fourums.adapter;

import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONException;
import org.kefirsf.bb.BBProcessorFactory;
import org.kefirsf.bb.TextProcessor;

import androidvn.xenforo.fourums.R;
import androidvn.xenforo.fourums.imgloader.SmiliesConverter;
import androidvn.xenforo.fourums.model.Post;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomPostListAdapter extends BaseAdapter {
	Context context;
	ArrayList<Post> postList;
	private Handler handler = new Handler();

	public CustomPostListAdapter(Context context, ArrayList<Post> threadList) {
		super();
		this.context = context;
		this.postList = threadList;
	}

	@Override
	public int getCount() {
		return postList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		ViewHolder holder = null;

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.post_list_item, viewGroup,
					false);

			holder = new ViewHolder();
			holder.ivAvatar = (ImageView) convertView
					.findViewById(R.id.ivAvatar);
			holder.tvUsername = (TextView) convertView
					.findViewById(R.id.tvUsername);
			holder.tvPosition = (TextView) convertView
					.findViewById(R.id.tvPosition);
			holder.tvPostDate = (TextView) convertView
					.findViewById(R.id.tvPostDate);
			holder.wvMessage = (WebView) convertView
					.findViewById(R.id.wvMessage);
			holder.wvMessage.setBackgroundColor(Color.parseColor("#E8E8E8"));
			// holder.tvLikes = (TextView)
			// convertView.findViewById(R.id.tvLikes);
			holder.tvLikeUsers = (TextView) convertView
					.findViewById(R.id.tvLikeUsers);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		//
		holder.tvUsername.setText(postList.get(position).getUserName());
		holder.tvPosition.setText("#" + postList.get(position).getPosition());
		holder.tvPostDate.setText(postList.get(position).getPostDate());

		// convert bb code
		try {
			SmiliesConverter.readFile(context);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		holder.wvMessage.getSettings().setJavaScriptEnabled(true);
		holder.wvMessage.addJavascriptInterface(new MyJavaScriptInterface(),
				"Android");
		holder.wvMessage.loadUrl("about:blank");
		TextProcessor processor = BBProcessorFactory.getInstance().create();
		String htmlCode = processor
				.process(postList.get(position).getMessage());
		htmlCode = SmiliesConverter.ConverteSmiles(htmlCode);
		holder.wvMessage.loadDataWithBaseURL("fake://not/needed", htmlCode,
				"text/html", "utf-8", "");
		// holder.tvLikes.setText(threadList.get(position).getLikes());
		holder.tvLikeUsers.setText(postList.get(position).getLikeUsers());
		return convertView;
	}

	final class MyJavaScriptInterface {
		public void ProcessJavaScript(final String scriptname, final String args) {
			handler.post(new Runnable() {
				public void run() { // Do your activities
				}
			});
		}
	}

	static class ViewHolder {
		TextView tvUsername, tvPosition, tvPostDate, tvLikes, tvLikeUsers;
		ImageView ivAvatar;
		WebView wvMessage;
	}
}
